<?php

class C_Request
{
	protected $_params = array();
	protected $_module = '';
	protected $_action = '';
	protected $_method = '';
	protected $_applicationRoot = '';
	protected $_formatModule = '';
	protected $_formatAction = '';
	protected $_formatMethod = '';
	public function __construct($applicationRoot)
	{
		$this->_applicationRoot = $applicationRoot;
	}
	public function getApplicationRoot()
	{
		return $this->_applicationRoot;
	}
	public function __get($key)
    {
//dhtmlspecialchars   	
//'&' (ampersand) becomes '&amp;' 
//
//'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set. 
//
//''' (single quote) becomes '&#039;' only when ENT_QUOTES is set. 
//
//'<' (less than) becomes '&lt;' 
//
//'>' (greater than) becomes '&gt;' 
//这些字符是单引号（'）、双引号（"）、反斜线（\）与 NUL（NULL 字符）。 daddslashes
    	
        switch (true) {
            case isset($this->_params[$key]):
                return dstripslashes($this->_params[$key]);
            case isset($_GET[$key]):
                return  dstripslashes($_GET[$key]);
            case isset($_POST[$key]):
                return  dstripslashes($_POST[$key]);
            case isset($_COOKIE[$key]):
                return  dstripslashes($_COOKIE[$key]);
            case ($key == 'REQUEST_URI'):
                return $this->getRequestUri();
            case isset($_SERVER[$key]):
                return $_SERVER[$key];
            case isset($_ENV[$key]):
                return $_ENV[$key];
            default:
                return null;
        }
    }
    public function get($key)
    {
        return $this->__get($key);
    }
    public function setParam($key, $value)
    {
        $key = (string) $key;

        if ((null === $value) && isset($this->_params[$key])) {
            unset($this->_params[$key]);
        } elseif (null !== $value) {
            $this->_params[$key] = $value;
        }

        return $this;
    }
    public function setParams(array $params)
    {
        foreach ($params as $key => $value) {
            $this->setParam($key, $value);
        }
        return $this;
    }

    public function getQuery($key = null, $default = null)
    {
        if (null === $key) {
            return $_GET;
        }

        return (isset($_GET[$key])) ? $_GET[$key] : $default;
    }

    public function getPost($key = null, $default = null)
    {
        if (null === $key) {
            return $_POST;
        }

        return (isset($_POST[$key])) ? $_POST[$key] : $default;
    }
	public function getCookie($key = null, $default = null)
    {
        if (null === $key) {
            return $_COOKIE;
        }

        return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default;
    }
    public function setModule($value, $default = null)
    {
    	if(empty($value))
    		$this->_module = $default;
    	else 
    		$this->_module = $value;
    }
  	public function setAction($value, $default = null)
    {
    	if(empty($value))
    		$this->_action = $default;
    	else 
    		$this->_action = $value;
    }
  	public function setMethod($value, $default = null)
    {
    	if(empty($value))
    		$this->_method = $default;
    	else 
    		$this->_method = $value;
    }
    public function getModule()
    {
		return $this->_module;
    }
  	public function getAction()
    {
		return $this->_action;
    }
  	public function getMethod()
    {
		return $this->_method;
    }
	public function getformatModule()
    {
    	return $this->_formatModule; 
    }
    public function getformatAction()
    {
    	return $this->_formatAction; 
    }
	public function getformatMethod()
    {
    	return $this->_formatMethod; 
    }
    public function setformatModule($formatModule)
    {
    	$this->_formatModule = $formatModule;
    }
    public function setformatAction($formatAction)
    {
    	$this->_formatAction = $formatAction;
    }
    public function setformatMethod($formatMethod)
    {
    	$this->_formatMethod = $formatMethod;
    }
    
	public function getRequestUri()
    {
    	return $_SERVER['REQUEST_URI'];
    }
    public function setCookie($name, $value, $expire, $path = '/' ,$domain,$secure = false)
    {
    	setcookie($name, $value, $expire, $path , $domain, $secure = false );
    }
    public function getDomain()
    {
    	$zend_config = Zend_Registry::get("zend_config");
    	$domain = $zend_config->web->domain;
    	return $domain;
    }
	public function getHost()
    {
//    	$zend_config = Zend_Registry::get("zend_config");
//    	$host = $zend_config->web->host;
		$host = $_SERVER['SERVER_NAME'];
    	return $host;
    }
    public function getReferer()
    {
    	return $_SERVER[HTTP_REFERER];
    }
	public function getIp()
	{
		return $_SERVER["REMOTE_ADDR"];  
	}
	public function decrypt($str)
	{
		return T_SimpleEncrypt::passport_decrypt($str);
	}
	public function encrypt($str)
	{
		return T_SimpleEncrypt::passport_encrypt($str);
	}
}
?>